#!/usr/bin/perl -w
=head1 NAME

 dust

=head1 SYNOPSIS

 A CGI script for serving data objects
 Copyright (C) 2007 Paul Schulz <paul@mawsonlakes.org>

 Based on code from apt-cacher.

 Copyright (C) 2005 Eduard Bloch <blade@debian.org>
 Distributed under the terms of the GNU Public Licence (GPL).

=head1 DESCRIPTION

 If you want to distribute files across a network, this proxy can be used
 to distribute files automatically.

 Dust is a CGI script that allows file to be retrieved according to
 their hash.

=head1 INSTALLATION

 Assuming your cache server is called B<www.myserver.com> and your
 cache directory is called B</var/cache/dust> then edit
 /etc/dust/dust.conf.

=cut

#############################################################################
### configuration ###########################################################
# Include the library for the config file parser
require "/usr/share/dust/dust-lib.pl";

# Read in the config file and set the necessary variables
my $config_file = '/etc/dust/dust.conf';
my $config_ref;
eval {
        $config_ref = read_config($config_file);
};
my %config = %$config_ref;

if (defined $ENV{PATH_INFO}){
    $object=$ENV{PATH_INFO};
    $object=~s/^\///;
    $object=~s/\/$//;
} else {
    $object="";
}

if ( $object eq "" ){
    $object = "intro";
}

# If not a SHA1 hash reference
if ( $object !~ /^[0-9a-f]{40}$/ ){
    $path = $config{'refs_dir'};
 
#    print "Content-type: text/plain\n\n";    
#    print "[$path][$object]\n";
    
    $o = `cat $path/$object`;
    chomp $o;
    $object=$o;

#    print "[$object]\n";
#    exit;

}
    
$path = $config{'store_dir'};

# Get mimetype
my $mimetype=`file -izb $path/$object`;
chomp $mimetype;

# Return file	
print "Content-type: $mimetype\n\n";    
open(IFILE,"cat $path/$object |");
while($line=<IFILE>){
    print $line;
}
close IFILE;

